<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" >

	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
	<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
	<link href="https://fonts.googleapis.com/css?family=Cousine|Inconsolata" rel="stylesheet">
	<link rel="shortcut icon" type="image/png" href="/favicon.png">

	
	<link rel="stylesheet" href="/css/style.css">
	<link rel="stylesheet" href="/css/css/fontawesome-all.min.css">
	<script src="/js/main.js"></script>
	

	<!-- Begin Jekyll SEO tag v2.1.0 -->
<title>PY: Get Voters List - Steem Developer</title>
<meta property="og:title" content="PY: Get Voters List" />
<meta name="description" content="This tutorial pulls a list of the voters on selected post from the blockchain" />
<meta property="og:description" content="This tutorial pulls a list of the voters on selected post from the blockchain" />
<link rel="canonical" href="https://developer.steem.io/tutorials-python/get_voters_list" />
<meta property="og:url" content="https://developer.steem.io/tutorials-python/get_voters_list" />
<meta property="og:site_name" content="Steem Developer" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2018-07-20T12:56:08-05:00" />
<script type="application/ld+json">
{"@context": "http://schema.org",
"@type": "BlogPosting",
"headline": "PY: Get Voters List",
"datePublished": "2018-07-20T12:56:08-05:00",
"description": "This tutorial pulls a list of the voters on selected post from the blockchain",
"url": "https://developer.steem.io/tutorials-python/get_voters_list"}</script>
<!-- End Jekyll SEO tag -->


	
</head>
<body>
<header>
	<h1>
		<button type="button" class="open-nav" id="open-nav"></button>
		<a href="/"  class="logo-link"><img src="/images/logotype_white.svg" height="40" alt="Steem Developer logo"></a>
	</h1>

	
	<form action="/search/" method="get">
		<input type="text" name="q" id="search-input" placeholder="Search">
		<input type="submit" value="Search" style="display: none;">
	</form>
	

	<div id="sidebar" class="sidebar">
	
	
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/">
		<h6 class="ctrl-nav-section title">Introduction</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/#introduction-welcome">Welcome to Steem</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/quickstart/">
		<h6 class="ctrl-nav-section title">Quickstart</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/quickstart/#quickstart-choose-library">Choose Library</a></li>
						
					
						
						<li><a href="/quickstart/#quickstart-steemd-nodes">steemd Nodes</a></li>
						
					
						
						<li><a href="/quickstart/#quickstart-testnet">Steem Testnet</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/tutorials/">
		<h6 class="ctrl-nav-section title">Tutorials</h6>
		<ul class="cont-nav-section content">
			
				
					
						<li><a href="/tutorials/#tutorials-javascript">Javascript</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-python">Python</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-ruby">Ruby</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-recipes">Recipes</a></li>
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/services/">
		<h6 class="ctrl-nav-section title">Services</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/services/#services-steemit">Steemit.com</a></li>
						
					
						
						<li><a href="/services/#services-steemconnect">SteemConnect</a></li>
						
					
						
						<li><a href="/services/#services-jussi">jussi</a></li>
						
					
						
						<li><a href="/services/#services-imagehoster">ImageHoster</a></li>
						
					
						
						<li><a href="/services/#services-sbds">SBDS</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/apidefinitions/">
		<h6 class="ctrl-nav-section title">Appbase API</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/apidefinitions/#apidefinitions-condenser-api">Condenser Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-account-by-key-api">Account By Key Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-broadcast-ops-comment">Broadcast Ops Comment</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-account-history-api">Account History Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-database-api">Database Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-follow-api">Follow Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-jsonrpc">Jsonrpc</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-market-history-api">Market History Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-network-broadcast-api">Network Broadcast Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-tags-api">Tags Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-witness-api">Witness Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-block-api">Block Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-broadcast-ops">Broadcast Ops</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/community/">
		<h6 class="ctrl-nav-section title">Community</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/community/#community-overview">Overview</a></li>
						
					
						
						<li><a href="/community/#community-resources">Resources</a></li>
						
					
						
						<li><a href="/community/#community-developeradvocate">Sharing & Help</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/glossary/">
		<h6 class="ctrl-nav-section title">Glossary</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/glossary/#glossary-chain-basics">Chain Basics</a></li>
						
					
						
					
						
						<li><a href="/glossary/#glossary-governance">Governance</a></li>
						
					
						
						<li><a href="/glossary/#glossary-transactions">Transactions</a></li>
						
					
						
						<li><a href="/glossary/#glossary-api">API</a></li>
						
					
						
						<li><a href="/glossary/#glossary-market">Market</a></li>
						
					
				
			
		</ul>
	</section>
	
</div>


	<!--<p class="copyright">-->
	<!--<a href="https://steemit.com.com/">-->
	<!--steemit-->
	<!--</a>-->
	<!--</p>-->
	<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
</header>
<div class="main" id="top">
	<div class="hero">
		<h1 class="hero__h1">Steem Developer Portal </h1>
		
		<img class="hero__img" src="/images/hero.png">
		
	</div>
	<section id="tutorials-python-get-voters-list" class="row tutorials-python-get-voters-list">
	<h3>
		<a id="tutorials-python-get-voters-list">
			PY: Get Voters List
			
		</a>
	</h3>
	
		<p class="description">This tutorial pulls a list of the voters on selected post from the blockchain</p>
	

	<p><span class="fa-pull-left top-of-tutorial-repo-link"><span class="first-word">Full</span>, runnable src of <a href="https://github.com/steemit/devportal-tutorials-py/tree/master/tutorials/03_get_voters_list">Get Voters List</a> can be downloaded as part of the <a href="https://github.com/steemit/devportal-tutorials-py">PY tutorials repository</a>.</span>
<br /></p>

<p>This tutorial will explain and show you how to access the <strong>Steem</strong> blockchain using the <a href="https://github.com/steemit/steem-python">steem-python</a> library to fetch list of posts and get voters info on selected post.</p>

<h2 id="intro">Intro</h2>

<p>All python tutorials will be in Python3 and developers following these tutorials should have sufficient knowlegde to use pip/pip3 package manager. Your development environment should be set up properly to test these tutorials.</p>

<p>Steem python library has built-in function to get active voters information if post with author and permlink as an argument. Since we don’t have predefined post or author/permlink. We will fetch post list from previous tutorial and give option to choose one post to get its active voters. <code class="highlighter-rouge">get_active_votes</code> function fetches list of active voters on content. Note that <code class="highlighter-rouge">get_discussions_by_active</code> filter is used for fetching 5 posts which by default contains <code class="highlighter-rouge">active_votes</code> of each post, but for purpose of this tutorial we will use <code class="highlighter-rouge">get_active_votes</code> function to fetch voters info.</p>

<h2 id="steps">Steps</h2>

<ol>
  <li><a href="#app-setup"><strong>App setup</strong></a> - Library install and import</li>
  <li><a href="#post-list"><strong>Post list</strong></a> - List of posts to select from created filter</li>
  <li><a href="#voters-list"><strong>Voters list</strong></a> - Get voters list for selected post</li>
  <li><a href="#print-output"><strong>Print output</strong></a> - Print results in output</li>
</ol>

<h4 id="1-app-setup-">1. App setup <a name="app-setup"></a></h4>

<p>In this tutorial we use 3 packages, <code class="highlighter-rouge">pick</code> - helps us to select filter interactively. <code class="highlighter-rouge">steem</code> - steem-python library, interaction with Blockchain. <code class="highlighter-rouge">pprint</code> - print results in better format.</p>

<p>First we import all three library and initialize Steem class</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>    <span class="kn">import</span> <span class="nn">pprint</span>
    <span class="kn">from</span> <span class="nn">pick</span> <span class="kn">import</span> <span class="n">pick</span>
    <span class="c"># initialize Steem class</span>
    <span class="kn">from</span> <span class="nn">steem</span> <span class="kn">import</span> <span class="n">Steem</span>

    <span class="n">s</span> <span class="o">=</span> <span class="n">Steem</span><span class="p">()</span>
</code></pre>
</div>

<h4 id="2-post-list-">2. Post list <a name="post-list"></a></h4>

<p>Next we will fetch and make list of posts and setup <code class="highlighter-rouge">pick</code> properly.</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>    <span class="n">query</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s">"limit"</span><span class="p">:</span><span class="mi">5</span><span class="p">,</span> <span class="c">#number of posts</span>
        <span class="s">"tag"</span><span class="p">:</span><span class="s">""</span> <span class="c">#tag of posts</span>
        <span class="p">}</span>
    <span class="c">#post list for selected query</span>
    <span class="n">posts</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get_discussions_by_active</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>

    <span class="n">title</span> <span class="o">=</span> <span class="s">'Please choose post: '</span>
    <span class="n">options</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="c">#posts list options</span>
    <span class="k">for</span> <span class="n">post</span> <span class="ow">in</span> <span class="n">posts</span><span class="p">:</span>
        <span class="n">options</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">post</span><span class="p">[</span><span class="s">"author"</span><span class="p">]</span><span class="o">+</span><span class="s">'/'</span><span class="o">+</span><span class="n">post</span><span class="p">[</span><span class="s">"permlink"</span><span class="p">])</span>
    <span class="c"># get index and selected filter name</span>
    <span class="n">option</span><span class="p">,</span> <span class="n">index</span> <span class="o">=</span> <span class="n">pick</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">title</span><span class="p">)</span>
</code></pre>
</div>

<p>This will show us list of posts to select in terminal/command prompt. And after selection we will get index and post name to <code class="highlighter-rouge">index</code> and <code class="highlighter-rouge">option</code> variables.</p>

<h4 id="3-voters-list-">3. Voters list <a name="voters-list"></a></h4>

<p>Next we will fetch active votes on selected post with <code class="highlighter-rouge">get_active_votes</code>. By default <code class="highlighter-rouge">get_discussions_by_active</code> function already contains <code class="highlighter-rouge">active_votes</code> list, but for this tutorial purpose we will ignore all other fields but only use <code class="highlighter-rouge">author</code> and <code class="highlighter-rouge">permlink</code> fields to fetch voters list.</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>
<span class="n">voters</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">get_active_votes</span><span class="p">(</span><span class="n">posts</span><span class="p">[</span><span class="n">index</span><span class="p">][</span><span class="s">"author"</span><span class="p">],</span><span class="n">posts</span><span class="p">[</span><span class="n">index</span><span class="p">][</span><span class="s">"permlink"</span><span class="p">])</span>
</code></pre>
</div>

<h4 id="4-print-output-">4. Print output <a name="print-output"></a></h4>

<p>Next, we will print result, details of selected post.</p>

<div class="language-python highlighter-rouge"><pre class="highlight"><code>    <span class="c"># print voters list for selected post</span>
    <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">voters</span><span class="p">)</span>
    <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="s">"Selected: "</span><span class="o">+</span><span class="n">option</span><span class="p">)</span>
</code></pre>
</div>

<p>The example of result returned from the service is a <code class="highlighter-rouge">JSON</code> object with the following properties:</p>

<div class="language-json highlighter-rouge"><pre class="highlight"><code><span class="p">[{</span><span class="err">'percent':</span><span class="w"> </span><span class="err">100,</span><span class="w">
  </span><span class="err">'reputation':</span><span class="w"> </span><span class="err">'4675452335798',</span><span class="w">
  </span><span class="err">'rshares':</span><span class="w"> </span><span class="err">174045922,</span><span class="w">
  </span><span class="err">'time':</span><span class="w"> </span><span class="err">'2018-06-13T05:27:06',</span><span class="w">
  </span><span class="err">'voter':</span><span class="w"> </span><span class="err">'ubg',</span><span class="w">
  </span><span class="err">'weight':</span><span class="w"> </span><span class="err">663</span><span class="p">},</span><span class="w">
 </span><span class="p">{</span><span class="err">'percent':</span><span class="w"> </span><span class="err">3000,</span><span class="w">
  </span><span class="err">'reputation':</span><span class="w"> </span><span class="err">0,</span><span class="w">
  </span><span class="err">'rshares':</span><span class="w"> </span><span class="err">'52213408920',</span><span class="w">
  </span><span class="err">'time':</span><span class="w"> </span><span class="err">'2018-06-13T12:53:30',</span><span class="w">
  </span><span class="err">'voter':</span><span class="w"> </span><span class="err">'warofcraft',</span><span class="w">
  </span><span class="err">'weight':</span><span class="w"> </span><span class="err">99589</span><span class="p">},</span><span class="w">
 </span><span class="p">{</span><span class="err">'percent':</span><span class="w"> </span><span class="err">10000,</span><span class="w">
  </span><span class="err">'reputation':</span><span class="w"> </span><span class="err">'16976056264304',</span><span class="w">
  </span><span class="err">'rshares':</span><span class="w"> </span><span class="err">'41495494555',</span><span class="w">
  </span><span class="err">'time':</span><span class="w"> </span><span class="err">'2018-06-13T08:56:00',</span><span class="w">
  </span><span class="err">'voter':</span><span class="w"> </span><span class="err">'jiahn',</span><span class="w">
  </span><span class="err">'weight':</span><span class="w"> </span><span class="err">80257</span><span class="p">}</span><span class="w">
</span><span class="p">]</span><span class="w">
</span><span class="err">'Selected:</span><span class="w"> </span><span class="err">steemitblog/join-team-steemit-at-tokenfest'</span><span class="w">
</span></code></pre>
</div>

<p>From this result you have access to everything associated to the voter including reputation of voter, timestamp, voter’s account name, percent and weight of vote, rshares reward shares values that you can be use in further development of application with Python.</p>

<p>That’s it!</p>

<h3 id="to-run-the-tutorial">To Run the tutorial</h3>

<ol>
  <li>clone this repo</li>
  <li><code class="highlighter-rouge">cd tutorials/03_get_voters_list</code></li>
  <li><code class="highlighter-rouge">pip install -r requirements.txt</code></li>
  <li><code class="highlighter-rouge">python index.py</code></li>
  <li>After a few moments, you should see output in terminal/command prompt screen.</li>
</ol>

<hr />

</section>

</div>
<div class="footer">
	<a href="#">Back to top</a>
</div>
<script>
    document.getElementById("open-nav").addEventListener("click", function () {
        document.body.classList.toggle("nav-open");
    });
</script>
</body>
</html>


<script src="/js/adjust.js"></script>

